package com.itheima.utils;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.RetryNTimes;

/**
 * @author leboy
 * @description
 */
public class InterProcessLockUtils {

	private InterProcessLockUtils(){}

	private static CuratorFramework client;
	private static InterProcessMutex lock;

	/**
	 *  得到锁
	 * @return
	 */
	public static InterProcessMutex getLock(){
		// 每一秒重试一次  重试15次
		RetryPolicy retryPolicy = new RetryNTimes(15,1000 );
		// client
		client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy);
		// 启动客户端
		client.start();
		// 构造锁
		return new InterProcessMutex(client, "/serviceLock");
	}

	/**
	 * 关闭client
	 */
	public static void closeClient(){
		client.close();
	}
}
